package main

import (
	"net/http"
	"wwp.space/NetSim/wjwt"

	"github.com/gin-gonic/gin"
	"wwp.space/NetSim/config"
	"wwp.space/NetSim/mideware"
)

type name int

func helloHandler(ctx *gin.Context) {
	ctx.JSON(http.StatusOK, gin.H{
		"message": "welcome, you pass",
	})
}

func loginHandler(ctx *gin.Context) {

	id, _ := ctx.Get("userId")

	token, _ := wjwt.GeneratedToken(map[string]interface{}{
		"userId": id,
	})
	ctx.JSON(http.StatusOK, gin.H{
		"token": token,
	})

}

func pictureReader(c *gin.Context) {
	response, err := http.Get("https://raw.githubusercontent.com/gin-gonic/logo/master/color.png")
	if err != nil || response.StatusCode != http.StatusOK {
		c.Status(http.StatusServiceUnavailable)
		return
	}

	reader := response.Body
	contentLength := response.ContentLength
	contentType := response.Header.Get("Content-Type")

	extraHeaders := map[string]string{
		"Content-Disposition": `attachment; filename="gopher.png"`,
	}

	c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
}

func main() {

	config.HelloWorld()

	r := gin.Default()

	r.Use(mideware.VertifyToken())

	r.GET("/hello", helloHandler)

	gb := r.Group("/login")
	gb.Use(mideware.UserCheck())

	gb.GET("/ssl", loginHandler)

	r.GET("/someDataFromReader", pictureReader)

	r.Run("0.0.0.0:9999")

}
